package com.ylxl.common.result;

import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import java.beans.PropertyEditorSupport;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * web层通用数据处理
 * @Author: Gilbert
 * @Date: 2021/11/9 16:39
 * @Description:
 */

public class BaseController {

    /**
     * 返回成功
     */
    public AjaxResult success()
    {
        return AjaxResult.success();
    }

    /**
     * 返回失败消息
     */
    public AjaxResult error()
    {
        return AjaxResult.error();
    }

    /**
     * 返回成功消息
     */
    public AjaxResult success(String message)
    {
        return AjaxResult.success(message);
    }

    /**
     * 返回失败消息
     */
    public AjaxResult error(String message)
    {
        return AjaxResult.error(message);
    }

    /**
     * 响应返回结果
     *
     * @param rows 影响行数
     * @return 操作结果
     */
    protected AjaxResult toAjax(int rows)
    {
        return rows > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 响应返回结果
     *
     * @param result 结果
     * @return 操作结果
     */
    protected AjaxResult toAjax(boolean result)
    {
        return result ? success() : error();
    }

    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(Date.class, new DateEditor());
    }


    public class DateEditor extends PropertyEditorSupport {
        public SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        public SimpleDateFormat dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        public void setAsText(String text){
            setValue(parseString(text));
        }
        private Object parseString(String text){
            Date date = null;
            if (null != text && !"".equals(text) && !"null".equals(text)) {
                try {
                    String newText = null;
                    if (text.contains("T") && text.contains("Z")) { // 处理2019-07-07T16:00:00.000Z时间格式
                        text = text.replace("Z", " UTC");
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
                        Date date1 = sdf.parse(text);
                        newText = dateFormat.format(date1);
                        newText = newText + " 00:00:00";
                    }else if(text.contains("GMT")){
                        text = text.replace("GMT", "").replaceAll("\\(.*\\)", "");
                        //将字符串转化为date类型，格式2016-10-12
                        SimpleDateFormat format =  new SimpleDateFormat("EEE MMM dd yyyy hh:mm:ss z", Locale.ENGLISH);
                        date = format.parse(text);
                    }
                    if(date == null){
                        dateTimeFormat.setLenient(false);
                        if (null != newText) {
                            date = dateTimeFormat.parse(newText);
                        } else {
                            date = dateTimeFormat.parse(text);
                        }

                    }
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                if (date == null) {
                    try {
                        dateFormat.setLenient(false);
                        date = dateFormat.parse(text);
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                }
            }
            return date;
        }
    }

}
